package org.example.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.example.dao.UserDoMapper;
import org.example.dao.UserPasswordDoMapper;
import org.example.datapbject.UserDo;
import org.example.datapbject.UserPasswordDo;
import org.example.error.BusinessException;
import org.example.error.EmBusinessError;
import org.example.service.UserService;
import org.example.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserPasswordDoMapper userPasswordDoMapper;
    @Autowired
    private UserDoMapper userDoMapper;

    @Override
    public UserModel getByUserid(Integer id) {
        //调用Userdomapper获取到对应的用户的dataobject
        UserDo userDo = userDoMapper.selectByPrimaryKey(id);
        if (userDo == null) {
            return null;
        }
        //通过用户Id获取对应的用户的加密密码信息
        UserPasswordDo userPasswordDo = userPasswordDoMapper.selectByUserId(userDo.getId());
        return convertFromEntity(userDo, userPasswordDo);
    }

    @Override
    @Transactional
    public void register(UserModel userModel) throws BusinessException {
        if (userModel == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        if (StringUtils.isEmpty(userModel.getName())
                || userModel.getAge() == null
                || userModel.getGender() == null
                || StringUtils.isEmpty(userModel.getTelphone())) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //实现model->dataobject方法
        UserDo userDo = convertFromModel(userModel);
        userDoMapper.insertSelective(userDo);

        UserPasswordDo userPasswordDo = converPasswordFromModel(userModel);
        userPasswordDoMapper.insertSelective(userPasswordDo);
        return;
    }

    @Override
    public UserModel login(String telphone, String encrptPassword) throws BusinessException {
        UserDo userDo = userDoMapper.selectByTelphone(telphone);
        if (userDo==null){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN);
        }
        UserPasswordDo userPasswordDo=userPasswordDoMapper.selectByUserId(userDo.getId());
        UserModel userModel=convertFromEntity(userDo,userPasswordDo);
        if (!StringUtils.equals(encrptPassword,userModel.getEncrptPassword())){
            throw new BusinessException(EmBusinessError.USER_NOT_LOGIN);
        }
        return userModel;
    }

    private UserPasswordDo converPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPasswordDo userPasswordDo = new UserPasswordDo();
        userPasswordDo.setEncrptPassword(userModel.getEncrptPassword());
        userPasswordDo.setId(userModel.getId());
        return userPasswordDo;

    }

    private UserDo convertFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserDo userDo = new UserDo();
        BeanUtils.copyProperties(userModel, userDo);
        return userDo;
    }

    private UserModel convertFromEntity(UserDo userDo, UserPasswordDo userPasswordDo) {
        if (userDo == null) {
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDo, userModel);
        if (userPasswordDo != null) {
            userModel.setEncrptPassword(userPasswordDo.getEncrptPassword());
        }
        return userModel;
    }
}
